package org.nuist.utils.ShellTool;

import com.alibaba.fastjson.JSONObject;
import org.nuist.utils.ShellTool.tool.ShellUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cheung0
 * Created on 2021/10/22
 */
public class Function {

    ShellUtil shellUtil = new ShellUtil();

    /**
     * @param: [VMID]
     * @return: void
     * 销毁虚拟机
     */
    public void destroyVM(String VMID) {

        String[] command = {"sh /root/shell/destroy.sh " + VMID};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Destroy VM:" + VMID + " successful!");

    }

    /**
     * @param: [VMID]
     * @return: void
     * 开启虚拟机
     */
    public void openVM(String VMID) {

        String[] command = {"sh /root/shell/up.sh " + VMID};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Open VM:" + VMID + " successful!");

    }

    /**
     * @param: [VMID]
     * @return: void
     * 关闭虚拟机
     */
    public void shutVM(String VMID) {

        String[] command = {"sh /root/shell/down.sh " + VMID};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Shut VM:" + VMID + " successful!");

    }

    /**
     * @param: [VMID]
     * @return: void
     * 重启虚拟机
     */
    public void rebootVM(String VMID) {

        String[] command = {"sh /root/shell/reboot.sh " + VMID};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Reboot VM:" + VMID + " successful!");

    }

    /**
     * @param: []
     * @return: com.alibaba.fastjson.JSONObject
     * 返回虚拟机有关信息
     */
    public JSONObject listVM() {

        String result = null;
        String[] command = {"sh /root/shell/list.sh"};
        JSONObject json = new JSONObject();
        for (String i : command) {

            List<Map> box = new ArrayList<>();
            result = shellUtil.shell(i);
            String[] array = result.split("\n");
            for (int j = 1; j < array.length; j++) {

                Map<String, String> map = new LinkedHashMap<>();

                char[] vmid = new char[4];
                array[j].getChars(6, 10, vmid, 0);
                map.put("VMID", String.valueOf(vmid).strip());

                char[] name = new char[14];
                array[j].getChars(11, 25, name, 0);
                map.put("NAME", String.valueOf(name).strip());

                char[] status = new char[7];
                array[j].getChars(32, 39, status, 0);
                map.put("STATUS", String.valueOf(status).strip());

                char[] mem = new char[4];
                array[j].getChars(43, 47, mem, 0);
                map.put("MEM", String.valueOf(mem).strip());

                char[] bootdisk = new char[5];
                array[j].getChars(61, 66, bootdisk, 0);
                map.put("BOOTDISK", String.valueOf(bootdisk).strip());

                char[] pid = new char[6];
                array[j].getChars(67, 73, pid, 0);
                map.put("PID", String.valueOf(pid).strip());

                box.add(map);

            }
            json.put("VMLISTS", box);
        }
        return json;
    }

    /**
     * @param: [VMID, SNAPNAME]
     * @return: void
     * 建立虚拟机快照
     */
    public void snapshotVM(String VMID, String SNAPNAME) {

        String[] command = {"sh /root/shell/snapshot.sh " + VMID + " " + SNAPNAME};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Snapshot VM:" + VMID + " as name:" + SNAPNAME + " successful!");

    }

    /**
     * @param: [VMID]
     * @return: JSONObject
     * 返回虚拟机快照信息
     */
    public JSONObject listsnapshotVM(String VMID) {

        String[] command = {"sh /root/shell/listSnapshot.sh " + VMID};
        String result = null;
        for (String i : command) {
            result = shellUtil.shell(i);
        }

        List<Map> box = new ArrayList<>();
        JSONObject json = new JSONObject();

        String[] array = result.split("\n");
        int count = 0;
        for(String i : array) {

            Map<String,String> map = new LinkedHashMap<>();

            if (!i.contains("current")) {

                int indexBegin = i.indexOf("`->");
                int indexEnd = i.indexOf("202");
                char[] name = new char[indexEnd-indexBegin-4];
                i.getChars(indexBegin+4,indexEnd,name,0);
                map.put("name",String.valueOf(name).strip());

                char[] time = new char[20];
                i.getChars(indexEnd,indexEnd+20,time,0);
                map.put("time",String.valueOf(time).strip());

                int indexOfdesc = indexEnd + 24;
                char[] description = new char[14];
                i.getChars(indexOfdesc,indexOfdesc+14,description,0);
                map.put("description",String.valueOf(description).strip());

                box.add(map);
                count++;
            } else {
                int indexcurrent = i.indexOf("`->");
                for(int j = 1; j < count; j++) {
                    if(array[j].indexOf("`->") == indexcurrent-2) {
                        int indexBegin = array[j].indexOf("`->");
                        int indexEnd = array[j].indexOf("202");
                        char[] name = new char[indexEnd-indexBegin-4];
                        array[j].getChars(indexBegin+4,indexEnd,name,0);
                        json.put("current",String.valueOf(name).strip());
                        break;
                    }
                }
            }
        }

        json.put("SNAPLISTS",box);
        return json;

    }

    /**
     * @param: [VMID][SNAPNAME]
     * @return: void
     * 快照回滚
     */
    public void rollbackSnapshot(String VMID, String SNAPNAME) {

        String[] command = {"sh /root/shell/rollback.sh " + VMID + " " + SNAPNAME};
        for (String i : command) {
            shellUtil.shell(i);
        }
        System.out.println("Rollback VM:" + VMID + " current to " + SNAPNAME + " successful!");

    }

}